<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<style>
    body{background-color: #FFFFFF}
    .change_page{background-color: #FFFFFF}
    td{padding: 0!important;}
    .xuan{text-align: center;width: 45px!important;}
    .xuanname{padding-left: 10px}
    #list_list .buttonnone{position: absolute;right: 0;padding: 0 6px!important;top: -1px;color: #707070;background-color: #fff;border: 1px solid #e6e6e6;}
    #list_list .buttonlv{position: absolute;right: 0;padding: 0 6px!important;top: -1px;background-color: #248BFE}
    #list_list .fenname{display: none}
</style>

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title">
        <div class="page-title-con">
            <span id="flowsetname">设置产值相关配置项</span>
            <div class="move_title"></div>
            <div class="page-title-opt-list">
                <button type="button" class="layui-btn layui-btn-sm btn-dark marginr5" lay-submit="" lay-filter="add">提交</button>
                <span id="page-close" class="page-close" onclick="layer_close()"><i class="layui-icon layui-icon-close"></i></span>
            </div>
        </div>
    </div>
    <div class="main-container">

        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>设置产值相关配置项</span></div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th  style="width: 45px"><input type="checkbox" class="xuanhang" lay-filter="xuanhang" lay-skin="primary"></th>
                            <th>名称</th>
                            <th>完成比率(留空默认为1)</th>
                            <th>税后价系数(留空默认为1)</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list" style="max-height: 500px"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>


    </div>
</form>
<script>

    var qinglist = parent.qinglist;
    var hang_key = getQueryVariable('key');
    var param_enginid = getQueryVariable('enginid');


    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        var hangObj;
        var hangStr =  $("#list_list .chan" , parent.document).eq(hang_key).text() || '';
        if(hangStr){
            hangObj = JSON.parse(hangStr);
        }else{
            hangObj = '';
        }

        var str = '';
        //清单行
        for (var key in qinglist) {
            var resArr = get_exist_hang(hangObj, 'qing', qinglist[key].id);
            if(resArr){
                if(!resArr.fenname){
                    resArr.fenname = [];
                }
                str += '<tr key="qing">';
                str += '<td class="xuan"><input type="checkbox" class="xuanhang" checked lay-skin="primary"></td>';
                str += '<td>';
                str += '<input type="hidden" class="layui-input qingid" value="'+qinglist[key].id+'">';
                str += '<span class="xuanname">'+qinglist[key].qingname+'</span>';
                if(resArr.fenname.length > 0){
                    str += '<button type="button" onclick="set_fenname('+qinglist[key].id+',\''+qinglist[key].children+'\')" class="layui-btn buttonlv fennamebut_'+qinglist[key].id+'">';
                    str += '<span>分部项</span><span class="fenname fenname_'+qinglist[key].id+'">'+JSON.stringify(resArr.fenname)+'</span>';
                    str += '</button>';
                }else{
                    str += '<button type="button" onclick="set_fenname('+qinglist[key].id+',\''+qinglist[key].children+'\')" class="layui-btn buttonnone fennamebut_'+qinglist[key].id+'">';
                    str += '<span>分部项</span><span class="fenname fenname_'+qinglist[key].id+'"></span>';
                    str += '</button>';
                }
                str += '</td>';
                str += '<td><input type="text" class="layui-input rate"  value="'+resArr.rate+'"></td>';
                str += '<td><input type="text" class="layui-input ratio"  value="'+resArr.ratio+'"></td>';
                if(resArr.explain == undefined){
                    str += '<td><input type="text" class="layui-input explain" value=""></td>';
                }else{
                    str += '<td><input type="text" class="layui-input explain" value="'+resArr.explain+'"></td>';
                }
                str += '</tr>';
            }else{
                str += '<tr key="qing">';
                str += '<td class="xuan"><input type="checkbox" class="xuanhang" lay-skin="primary"></td>';
                str += '<td>';
                str += '<input type="hidden" class="layui-input qingid" value="'+qinglist[key].id+'">';
                str += '<span class="xuanname">'+qinglist[key].qingname+'</span>';
                str += '<button type="button" onclick="set_fenname('+qinglist[key].id+',\''+qinglist[key].children+'\')" class="layui-btn buttonnone fennamebut_'+qinglist[key].id+'">';
                str += '<span>分部项</span><span class="fenname fenname_'+qinglist[key].id+'"></span>';
                str += '</button>';
                str += '</td>';
                str += '<td><input type="text" class="layui-input rate" ></td>';
                str += '<td><input type="text" class="layui-input ratio" ></td>';
                str += '<td><input type="text" class="layui-input explain"></td>';
                str += '</tr>';
            }
        }

        //间接费
        var resArr = get_exist_hang(hangObj, 'jian', 'chui');
        if(resArr){
            str += '<tr key="jian">';
            str += '<td class="xuan"><input type="checkbox" class="xuanhang" checked lay-skin="primary"></td>';
            str += '<td>';
            str += '<input type="hidden" class="layui-input jiantype" value="chui">';
            str += '<span class="xuanname">垂直运输费</span>';
            str += '</td>';
            str += '<td><input type="text" class="layui-input rate"  value="'+resArr.rate+'"></td>';
            str += '<td><input type="text" class="layui-input ratio"  value="'+resArr.ratio+'"></td>';
            if(resArr.explain == undefined){
                str += '<td><input type="text" class="layui-input explain" value=""></td>';
            }else{
                str += '<td><input type="text" class="layui-input explain" value="'+resArr.explain+'"></td>';
            }
            str += '</tr>';
        }else{
            str += '<tr key="jian">';
            str += '<td class="xuan"><input type="checkbox" class="xuanhang" lay-skin="primary"></td>';
            str += '<td>';
            str += '<input type="hidden" class="layui-input jiantype" value="chui">';
            str += '<span class="xuanname">垂直运输费</span>';
            str += '</td>';
            str += '<td><input type="text" class="layui-input rate" ></td>';
            str += '<td><input type="text" class="layui-input ratio" ></td>';
            str += '<td><input type="text" class="layui-input explain"></td>';
            str += '</tr>';
        }

        var resArr = get_exist_hang(hangObj, 'jian', 'cuo');
        if(resArr){
            str += '<tr key="jian">';
            str += '<td class="xuan"><input type="checkbox" class="xuanhang" checked lay-skin="primary"></td>';
            str += '<td>';
            str += '<input type="hidden" class="layui-input jiantype" value="cuo">';
            str += '<span class="xuanname">措施费</span>';
            str += '</td>';
            str += '<td><input type="text" class="layui-input rate"  value="'+resArr.rate+'"></td>';
            str += '<td><input type="text" class="layui-input ratio"  value="'+resArr.ratio+'"></td>';
            if(resArr.explain == undefined){
                str += '<td><input type="text" class="layui-input explain" value=""></td>';
            }else{
                str += '<td><input type="text" class="layui-input explain" value="'+resArr.explain+'"></td>';
            }
            str += '</tr>';
        }else{
            str += '<tr key="jian">';
            str += '<td class="xuan"><input type="checkbox" class="xuanhang" lay-skin="primary"></td>';
            str += '<td>';
            str += '<input type="hidden" class="layui-input jiantype" value="cuo">';
            str += '<span class="xuanname">措施费</span>';
            str += '</td>';
            str += '<td><input type="text" class="layui-input rate" ></td>';
            str += '<td><input type="text" class="layui-input ratio" ></td>';
            str += '<td><input type="text" class="layui-input explain"></td>';
            str += '</tr>';
        }

        var resArr = get_exist_hang(hangObj, 'jian', 'gong');
        if(resArr){
            str += '<tr key="jian">';
            str += '<td class="xuan"><input type="checkbox" class="xuanhang" checked lay-skin="primary"></td>';
            str += '<td>';
            str += '<input type="hidden" class="layui-input jiantype" value="gong">';
            str += '<span class="xuanname">管理人员工资</span>';
            str += '</td>';
            str += '<td><input type="text" class="layui-input rate"  value="'+resArr.rate+'"></td>';
            str += '<td><input type="text" class="layui-input ratio"  value="'+resArr.ratio+'"></td>';
            if(resArr.explain == undefined){
                str += '<td><input type="text" class="layui-input explain" value=""></td>';
            }else{
                str += '<td><input type="text" class="layui-input explain" value="'+resArr.explain+'"></td>';
            }
            str += '</tr>';
        }else{
            str += '<tr key="jian">';
            str += '<td class="xuan"><input type="checkbox" class="xuanhang" lay-skin="primary"></td>';
            str += '<td>';
            str += '<input type="hidden" class="layui-input jiantype" value="gong">';
            str += '<span class="xuanname">管理人员工资</span>';
            str += '</td>';
            str += '<td><input type="text" class="layui-input rate" ></td>';
            str += '<td><input type="text" class="layui-input ratio" ></td>';
            str += '<td><input type="text" class="layui-input explain"></td>';
            str += '</tr>';
        }

        var resArr = get_exist_hang(hangObj, 'jian', 'guan');
        if(resArr){
            str += '<tr key="jian">';
            str += '<td class="xuan"><input type="checkbox" class="xuanhang" checked lay-skin="primary"></td>';
            str += '<td>';
            str += '<input type="hidden" class="layui-input jiantype" value="guan">';
            str += '<span class="xuanname">管理费</span>';
            str += '</td>';
            str += '<td><input type="text" class="layui-input rate"  value="'+resArr.rate+'"></td>';
            str += '<td><input type="text" class="layui-input ratio"  value="'+resArr.ratio+'"></td>';
            if(resArr.explain == undefined){
                str += '<td><input type="text" class="layui-input explain" value=""></td>';
            }else{
                str += '<td><input type="text" class="layui-input explain" value="'+resArr.explain+'"></td>';
            }
            str += '</tr>';
        }else{
            str += '<tr key="jian">';
            str += '<td class="xuan"><input type="checkbox" class="xuanhang" lay-skin="primary"></td>';
            str += '<td>';
            str += '<input type="hidden" class="layui-input jiantype" value="guan">';
            str += '<span class="xuanname">管理费</span>';
            str += '</td>';
            str += '<td><input type="text" class="layui-input rate" ></td>';
            str += '<td><input type="text" class="layui-input ratio" ></td>';
            str += '<td><input type="text" class="layui-input explain"></td>';
            str += '</tr>';
        }

        var resArr = get_exist_hang(hangObj, 'jian', 'yuzhi');
        if(resArr){
            str += '<tr key="jian">';
            str += '<td class="xuan"><input type="checkbox" class="xuanhang" checked lay-skin="primary"></td>';
            str += '<td>';
            str += '<input type="hidden" class="layui-input jiantype" value="yuzhi">';
            str += '<span class="xuanname">预置表1</span>';
            str += '</td>';
            str += '<td><input type="text" class="layui-input rate"  value="'+resArr.rate+'" ></td>';
            str += '<td><input type="text" class="layui-input ratio"  value="'+resArr.ratio+'" ></td>';
            if(resArr.explain == undefined){
                str += '<td><input type="text" class="layui-input explain" value=""></td>';
            }else{
                str += '<td><input type="text" class="layui-input explain" value="'+resArr.explain+'"></td>';
            }
            str += '</tr>';
        }else{
            str += '<tr key="jian">';
            str += '<td class="xuan"><input type="checkbox" class="xuanhang" lay-skin="primary"></td>';
            str += '<td>';
            str += '<input type="hidden" class="layui-input jiantype" value="yuzhi">';
            str += '<span class="xuanname">预置表1</span>';
            str += '</td>';
            str += '<td><input type="text" class="layui-input rate" ></td>';
            str += '<td><input type="text" class="layui-input ratio" ></td>';
            str += '<td><input type="text" class="layui-input explain"></td>';
            str += '</tr>';
        }

        var resArr = get_exist_hang(hangObj, 'jian', 'yuzhitwo');
        if(resArr){
            str += '<tr key="jian">';
            str += '<td class="xuan"><input type="checkbox" class="xuanhang" checked lay-skin="primary"></td>';
            str += '<td>';
            str += '<input type="hidden" class="layui-input jiantype" value="yuzhitwo">';
            str += '<span class="xuanname">预置表2</span>';
            str += '</td>';
            str += '<td><input type="text" class="layui-input rate"  value="'+resArr.rate+'"></td>';
            str += '<td><input type="text" class="layui-input ratio"  value="'+resArr.ratio+'"></td>';
            if(resArr.explain == undefined){
                str += '<td><input type="text" class="layui-input explain" value=""></td>';
            }else{
                str += '<td><input type="text" class="layui-input explain" value="'+resArr.explain+'"></td>';
            }
            str += '</tr>';
        }else{
            str += '<tr key="jian">';
            str += '<td class="xuan"><input type="checkbox" class="xuanhang" lay-skin="primary"></td>';
            str += '<td>';
            str += '<input type="hidden" class="layui-input jiantype" value="yuzhitwo">';
            str += '<span class="xuanname">预置表2</span>';
            str += '</td>';
            str += '<td><input type="text" class="layui-input rate" ></td>';
            str += '<td><input type="text" class="layui-input ratio" ></td>';
            str += '<td><input type="text" class="layui-input explain"></td>';
            str += '</tr>';
        }


        //投标价金额
        var resArr = get_exist_hang(hangObj, 'tou', '');
        if(resArr){
            str += '<tr key="tou">';
            str += '<td class="xuan"><input type="checkbox" class="xuanhang" checked lay-skin="primary"></td>';
            str += '<td><span class="xuanname">投标价金额</span></td>';
            str += '<td><input type="text" class="layui-input rate"  value="'+resArr.rate+'"></td>';
            str += '<td><input type="text" class="layui-input ratio"  value="'+resArr.ratio+'"></td>';
            if(resArr.explain == undefined){
                str += '<td><input type="text" class="layui-input explain" value=""></td>';
            }else{
                str += '<td><input type="text" class="layui-input explain" value="'+resArr.explain+'"></td>';
            }
            str += '</tr>';
        }else{
            str += '<tr key="tou">';
            str += '<td class="xuan"><input type="checkbox" class="xuanhang" lay-skin="primary"></td>';
            str += '<td><span class="xuanname">投标价金额</span></td>';
            str += '<td><input type="text" class="layui-input rate" ></td>';
            str += '<td><input type="text" class="layui-input ratio" ></td>';
            str += '<td><input type="text" class="layui-input explain"></td>';
            str += '</tr>';
        }



        //其他费用项
        var resArr = get_exist_hang(hangObj, 'qi', '1');
        if(resArr){
            str += '<tr key="qi">';
            str += '<td class="xuan"><input type="checkbox" class="xuanhang" checked lay-skin="primary"></td>';
            str += '<td>';
            str += '<span class="xuanname">其他费用项1</span>';
            str += '<input type="hidden" class="layui-input qitype" value="1">';
            str += '</td>';
            str += '<td colspan="2"><input type="text" class="layui-input rate" value="'+resArr.rate+'" placeholder="可直接填入金额或计算公式（以=开头）"></td>';
            if(resArr.explain == undefined){
                str += '<td><input type="text" class="layui-input explain" value=""></td>';
            }else{
                str += '<td><input type="text" class="layui-input explain" value="'+resArr.explain+'"></td>';
            }
            str += '</tr>';
        }else{
            str += '<tr key="qi">';
            str += '<td class="xuan"><input type="checkbox" class="xuanhang" lay-skin="primary"></td>';
            str += '<td>';
            str += '<span class="xuanname">其他费用项1</span>';
            str += '<input type="hidden" class="layui-input qitype" value="1">';
            str += '</td>';
            str += '<td colspan="2"><input type="text" class="layui-input rate" placeholder="可直接填入金额或计算公式（以=开头）"></td>';
            str += '<td><input type="text" class="layui-input explain"></td>';
            str += '</tr>';
        }

        var resArr = get_exist_hang(hangObj, 'qi', '2');
        if(resArr){
            str += '<tr key="qi">';
            str += '<td class="xuan"><input type="checkbox" class="xuanhang" checked lay-skin="primary"></td>';
            str += '<td>';
            str += '<span class="xuanname">其他费用项2</span>';
            str += '<input type="hidden" class="layui-input qitype" value="2">';
            str += '</td>';
            str += '<td colspan="2"><input type="text" class="layui-input rate" value="'+resArr.rate+'" placeholder="可直接填入金额或计算公式（以=开头）"></td>';
            if(resArr.explain == undefined){
                str += '<td><input type="text" class="layui-input explain" value=""></td>';
            }else{
                str += '<td><input type="text" class="layui-input explain" value="'+resArr.explain+'"></td>';
            }
            str += '</tr>';
        }else{
            str += '<tr key="qi">';
            str += '<td class="xuan"><input type="checkbox" class="xuanhang" lay-skin="primary"></td>';
            str += '<td>';
            str += '<span class="xuanname">其他费用项2</span>';
            str += '<input type="hidden" class="layui-input qitype" value="2">';
            str += '</td>';
            str += '<td colspan="2"><input type="text" class="layui-input rate" placeholder="可直接填入金额或计算公式（以=开头）"></td>';
            str += '<td><input type="text" class="layui-input explain"></td>';
            str += '</tr>';
        }

        $("#list_list").append(str);
        render_from();

        form.on('checkbox(xuanhang)',function (data) {
            if($(data.elem).prop("checked")){
                $('#list_list .xuanhang').prop("checked", true);
            }else{
                $('#list_list .xuanhang').prop("checked", false);
            }
            render_from();
        });


        form.on('submit(add)', function(data){
            var data = [];
            $('#list_list tr').each(function (i) {
                var checked = $(this).find('.xuanhang').prop("checked");
                var trkey = $(this).attr('key');
                var qingid = $(this).find('.qingid').val() || '';
                var jiantype = $(this).find('.jiantype').val() || '';
                var qitype = $(this).find('.qitype').val() || '';
                var rate = $(this).find('.rate').val() || '';
                var ratio = $(this).find('.ratio').val() || '';
                var explain = $(this).find('.explain').val() || '';

                if(checked){
                    if(trkey == 'qing'){
                        var fenname = $(this).find('.fenname_'+qingid).text() || '';
                        try{
                            var fennamearr = JSON.parse(fenname);
                        }catch (e) {
                            var fennamearr = [];
                        }
                        data.push({key: trkey, resvalue:qingid, rate:rate, ratio:ratio,explain:explain, fenname:fennamearr});
                    }
                    if(trkey == 'jian'){
                        data.push({key: trkey, resvalue:jiantype, rate:rate, ratio:ratio,explain:explain});
                    }
                    if(trkey == 'tou'){
                        data.push({key: trkey, resvalue:'', rate:rate, ratio:ratio,explain:explain});
                    }
                    if(trkey == 'qi'){
                        data.push({key: trkey, resvalue:qitype, rate:rate, ratio:ratio,explain:explain});
                    }
                }
            });

            if(data.length > 0){
                $("#list_list .chanbut" , parent.document).eq(hang_key).addClass('buttonlv').removeClass('buttonnone');
                var datastr = JSON.stringify(data);
            }else{
                $("#list_list .chanbut" , parent.document).eq(hang_key).addClass('buttonnone').removeClass('buttonlv');
                var datastr = '';
            }

            $("#list_list .chan" , parent.document).eq(hang_key).text(datastr);
            layer_close();
            return false;
        });

    });


    $('#list_list').on('focus','.rate,.ratio',function () {
        var val = $(this).val();
        if(val == ''){
            $(this).val('=');
        }
    });
    $('#list_list').on('blur','.rate,.ratio',function () {
        var val = $(this).val();
        if(val == '='){
            $(this).val('');
        }
    });

    var CurrentFennameArr;
    function set_fenname(qingid, children) {
        CurrentFennameArr = children.split(",");
        openPage('huizi_set_fenname.php?qingid='+qingid+'&enginid='+param_enginid, '90%', '90%');
    }


    function get_exist_hang(hangObj, key, resvalue) {
        var data = [];
        var exist = false;
        for (var ii in hangObj) {
            if(hangObj[ii].key == key && hangObj[ii].resvalue == resvalue){
                data = hangObj[ii];
                exist = true;
            }
        }
        if(exist){
            return data;
        }
        return exist;
    }
</script>
</body>
</html>
